package com.aplink.generic.widgets;

import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.ViewPagerAdapter;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.viewpager.ViewPagerSpec;
import com.aplink.sg.R;

public class ApLinkViewPager extends ApLinkBaseView {
    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_ALPHA = 0.5f;
        private static final float MIN_SCALE = 0.85f;

        @Override
        public void transformPage(final View view, final float position) {
            final int pageWidth = view.getWidth();
            final int pageHeight = view.getHeight();

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as
                // well
                final float scaleFactor = Math.max(
                        ZoomOutPageTransformer.MIN_SCALE,
                        1 - Math.abs(position));
                final float vertMargin = (pageHeight * (1 - scaleFactor)) / 2;
                final float horzMargin = (pageWidth * (1 - scaleFactor)) / 2;
                if (position < 0) {
                    view.setTranslationX(horzMargin - (vertMargin / 2));
                } else {
                    view.setTranslationX(-horzMargin + (vertMargin / 2));
                }

                // Scale the page down (between MIN_SCALE and 1)
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                // Fade the page relative to its size.
                view.setAlpha(ZoomOutPageTransformer.MIN_ALPHA
                        + (((scaleFactor - ZoomOutPageTransformer.MIN_SCALE) / (1 - ZoomOutPageTransformer.MIN_SCALE)) * (1 - ZoomOutPageTransformer.MIN_ALPHA)));

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    /**
     * 
     */
    private static final long serialVersionUID = -8525458644892383426L;

    private PagerAdapter genericViewPagerAdapter = null;

    private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageScrolled(final int position, final float arg1,
                final int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(final int position) {

        }

        @Override
        public void onPageSelected(final int position) {
            if (ApLinkViewPager.this.mViewPager.getAdapter() instanceof FragmentPagerAdapter) {
                final FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) ApLinkViewPager.this.mViewPager
                        .getAdapter();
                final GenericFragment genericFragment = (GenericFragment) fragmentPagerAdapter
                        .getItem(position);
                genericFragment.invalidateOptionsMenu();
                genericFragment.notifyDataSetChanged();
            }
        }
    };

    private final ViewPagerSpec mPagerFormat;

    private final ViewPager mViewPager;

    private int selectedIndex = 0;

    public ApLinkViewPager(final GenericFragment genericFragment,
            final ControlSpec controlSpec, final View view) {
        super(genericFragment, controlSpec, view);
        this.mViewPager = (ViewPager) view;
        this.mPagerFormat = (ViewPagerSpec) controlSpec;
    }

    @Override
    public void clear() {

    }

    private void createPagerTitleStrip() {
        final PagerTitleStrip pagerTitleStrip = new PagerTitleStrip(
                getApplicationContext());
        final ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
        layoutParams.height = LayoutParams.WRAP_CONTENT;
        layoutParams.width = LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.TOP;
        pagerTitleStrip.setBackgroundColor(getResources().getColor(
                R.color.aplink__generic_viewpager_title_background_color));
        final int height = getResources().getDimensionPixelSize(
                R.dimen.aplink__generic_viewpager_title_height_color);
        pagerTitleStrip.setPadding(0, height, 0, height);
        pagerTitleStrip.setTextColor(getResources().getColor(
                R.color.aplink__generic_viewpager_title_text_color));
        this.mViewPager.addView(pagerTitleStrip, layoutParams);

    }

    private void createViewPagerTab() {
        final PagerTabStrip pagerTitleStrip = new PagerTabStrip(
                getApplicationContext());
        final ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
        layoutParams.height = LayoutParams.WRAP_CONTENT;
        layoutParams.width = LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.TOP;
        pagerTitleStrip.setBackgroundColor(getResources().getColor(
                R.color.aplink__generic_viewpager_title_background_color));
        final int height = getResources().getDimensionPixelSize(
                R.dimen.aplink__generic_viewpager_title_height_color);
        pagerTitleStrip.setPadding(0, height, 0, height);
        pagerTitleStrip.setTextColor(getResources().getColor(
                R.color.aplink__generic_viewpager_title_text_color));
        pagerTitleStrip.setDrawFullUnderline(true);
        pagerTitleStrip.setTabIndicatorColor(getResources().getColor(
                R.color.aplink__generic_viewpager_tab_underline_color));
        this.mViewPager.addView(pagerTitleStrip, layoutParams);
    }

    @Override
    public GenericAttribute getAttribute() {
        return null;
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public ViewPager getView() {
        return this.mViewPager;
    }

    @Override
    protected void initViewProperties() {
        final String headerStyle = this.mPagerFormat.getHeaderStyle();
        if ((headerStyle != null) && headerStyle.equalsIgnoreCase("tab")) {
            createViewPagerTab();
        } else if ((headerStyle != null)
                && headerStyle.equalsIgnoreCase("title")) {
            createPagerTitleStrip();
        }

        this.genericViewPagerAdapter = new ViewPagerAdapter(
                getChildFragmentManager(), this.mPagerFormat);
        if (this.mPagerFormat.getOffscreenPageLimit() > 0) {
            this.mViewPager.setOffscreenPageLimit(this.mPagerFormat
                    .getOffscreenPageLimit());
        }
        if (this.mPagerFormat.isHasPageTransformer()) {
            this.mViewPager.setPageTransformer(true,
                    new ZoomOutPageTransformer());
        }
        this.selectedIndex = this.mPagerFormat.getDefaultTargetIndex();
        this.mViewPager.setCurrentItem(this.selectedIndex);
    }

    @Override
    protected void loadBindingObject() {
    }

    @Override
    protected void loadDefaultObject() {
        this.mViewPager.setAdapter(this.genericViewPagerAdapter);
        if (this.mViewPager.getCurrentItem() != this.selectedIndex) {
            this.mViewPager.setCurrentItem(this.selectedIndex);
        }
    }

    public void nextPage() {
        final int nextPage = this.mViewPager.getCurrentItem() + 1;
        final int maxPage = this.genericViewPagerAdapter.getCount();
        if (nextPage < maxPage) {
            this.mViewPager.setCurrentItem(nextPage, true);
        }
    }

    public void previousPage() {
        final int previousPage = this.mViewPager.getCurrentItem() - 1;
        if (previousPage >= 0) {
            this.mViewPager.setCurrentItem(previousPage, true);
        }
    }

    @Override
    protected void setEventListener() {
        this.mViewPager.setOnPageChangeListener(this.mOnPageChangeListener);
    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }
}
